package com.sbpm.crmbpm.client.grid;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.sbpm.crmbpm.client.providers.CompanyProperties;
import com.sbpm.crmbpm.shared.data.client.Company;
import com.sbpm.crmbpm.shared.data.client.CompanyType;
import com.sbpm.crmbpm.shared.data.client.Segment;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;

import java.util.ArrayList;
import java.util.List;

public class CompanyColumnModelFactory
{
    public static final CompanyProperties PROPS = GWT.create( CompanyProperties.class );

    public static ColumnModel<Company> createClientColumnModel()
    {


        ColumnConfig<Company, Long> idCol = new ColumnConfig<Company, Long>( PROPS.id(), 100, "ID");
        idCol.setHidden( true );
        ColumnConfig<Company, String> nameCol = new ColumnConfig<Company, String>( PROPS.name(), 250, "Наименование");
        ColumnConfig<Company, String> legalNameCol = new ColumnConfig<Company, String>( PROPS.legalName(), 250, "Юридическое наименование");
        ColumnConfig<Company, String> innCol = new ColumnConfig<Company, String>( PROPS.inn(), 100, "ИНН");
        ColumnConfig<Company, String> kppCol = new ColumnConfig<Company, String>( PROPS.kpp(), 150, "КПП");
        ColumnConfig<Company, String> ogrnCol = new ColumnConfig<Company, String>( PROPS.ogrn(), 150, "ОГРН");
        ColumnConfig<Company, CompanyType> typeCol = new ColumnConfig<Company, CompanyType>( PROPS.type(), 150, "Тип");
        ColumnConfig<Company, Segment> segmentCol = new ColumnConfig<Company, Segment>( PROPS.segment(), 150, "Сегмент");
        segmentCol.setCell( new AbstractCell< Segment >()
        {
            @Override
            public void render( Context context, Segment value, SafeHtmlBuilder sb )
            {
                if( value != null )
                {
                    sb.appendEscaped( value.getLabel() );
                }
            }
        } );

        /*ColumnConfig<TaskInfo, Double> changeCol = new ColumnConfig<TaskInfo, Double>(props.change(), 100, "Change");
        changeCol.setCell(new AbstractCell<Double>() {

            @Override
            public void render(Context context, Double value, SafeHtmlBuilder sb) {
                String style = "style='color: " + (value < 0 ? "red" : "green") + "'";
                String v = number.format(value);
                sb.appendHtmlConstant("<span " + style + " qtitle='Change' qtip='" + v + "'>" + v + "</span>");
            }
        });*/

        /*ColumnConfig<TaskInfo, Date> lastTransCol = new ColumnConfig<TaskInfo, Date>(props.lastTrans(), 100, "Last Updated");
        lastTransCol.setCell(new DateCell(DateTimeFormat.getFormat("MM/dd/yyyy")));*/

        List<ColumnConfig<Company, ?>> columnConfigs = new ArrayList<ColumnConfig<Company, ?>>();
        columnConfigs.add( idCol );
        columnConfigs.add( nameCol );
        columnConfigs.add( legalNameCol );
        columnConfigs.add( segmentCol );
        columnConfigs.add( innCol );
        columnConfigs.add( kppCol );
        columnConfigs.add( ogrnCol );
        ColumnModel<Company> columnModel = new ColumnModel<Company>(columnConfigs);
        return columnModel;
    }
}
